var express = require('express');
var router = express.Router();
var mysql = require('mysql');
const { JwtSend, mysqlCreate } = require('./utils');

//连接数据库
let conn = mysqlCreate();

//用户注册
router.post('/', (req, res) =>{
  //连接数据库配重置配置
  conn = mysql.createConnection(conn.config);
  //连接数据库
  conn.connect();
  const {username, password} = req.body;
  // 查询sql语句
  var selsql = `select * from user where name='${username}'`;

  //检查用户登录的的数据库中是否存在,并且进行判断
  conn.query(selsql, (err,result)=>{
    if(err){
      console.log(err)
      return
    }else{
      if(result[0]){
        if(result[0].password == password){
          var data = JwtSend(result);
          res.json({code: 10000, message: "登陆成功" ,content: data})
          return;
        }
      }else{
        res.json({code: -1, message: "此用户名未被注册请进行注册"})
      }
    }
  })
  conn.end();
})

module.exports = router;